package com.leyou.auth.service;

import com.leyou.auth.client.UserClient;
import com.leyou.auth.config.JwtProperties;
import com.leyou.auth.entity.UserInfo;
import com.leyou.auth.utils.JwtUtils;
import com.leyou.user.pojo.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Service;

/**
 * @author 岩冰
 * @version 1.0
 * @date:2018/10/18
 */
@Service
@EnableConfigurationProperties(JwtProperties.class)
public class AuthService {
    @Autowired
    private UserClient userClient;

    @Autowired
    private JwtProperties prop;
    private static final Logger LOGGER = LoggerFactory.getLogger(AuthService.class);
    public String authentication(String username,String password) {
        try {
            User user = userClient.queryUser(username, password);
            UserInfo userInfo = new UserInfo();
            BeanUtils.copyProperties(user, userInfo);
            String token = JwtUtils.generateToken(userInfo, prop.getPrivateKey(), prop.getExpire());
            return token;
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("查询usersevice服务器出错",e);
            return null;
        }
    }
}
